﻿define(function (require, exports, module) {
    require('plugins/fuelux.tree');
    //公有属性和方法
    module.exports = {
        init: function (jq, params) {
            jq.ace_tree({
                dataSource: getTreeData(params.dataSource)["dataSource1"],
                multiSelect: true,
                cacheItems: true,
                'open-icon': 'ace-icon tree-minus',
                'close-icon': 'ace-icon tree-plus',
                'selectable': false,
                'folderSelect': false,
                'ignoreRedundantOpens':false,
                'selected-icon': 'ace-icon fa fa-check',
                'unselected-icon': 'ace-icon fa fa-times',
                loadingHTML: '<div class="tree-loading"><i class="ace-icon fa fa-refresh fa-spin blue"></i></div>'
            }).on('loaded.fu.tree', function (event, data) {

            }).on('selected.fu.tree', function (event, data) {
                console.log(data);
            }).tree('discloseAll');
        }
    }

    function getTreeData(tree_data) {
        var dataSource1 = function (options, callback) {
            var $data = null
            if (!("text" in options) && !("type" in options)) {
                $data = tree_data;//the root tree
                callback({ data: $data });
                return;
            }
            else if ("type" in options && options.type == "folder") {
                if ("additionalParameters" in options && "children" in options.additionalParameters)
                    $data = options.additionalParameters.children || {};
                else $data = {}//no data
            }

            if ($data != null)//this setTimeout is only for mimicking some random delay
                setTimeout(function () { callback({ data: $data }); }, parseInt(Math.random() * 500) + 200);
        }
        return { 'dataSource1': dataSource1 }
    }
});
